package com.tran.app.screen

import android.text.TextUtils
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.BlendMode.Companion.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.room.Query
import com.orhanobut.logger.Logger
import com.tran.app.viewmodel.SCViewModel


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ShowCourseScreen(padding: Modifier, navCotroller: NavHostController,query: String) {
    Scaffold(modifier = Modifier.fillMaxSize(),topBar = {
        TopAppBar(title = { Text(text = "所有课程")},
            navigationIcon = {
                IconButton(onClick = { navCotroller.popBackStack() }) {
                    Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "back")
                }
            })
    }) {padding->
        Logger.d("ShowCourseScreen")
        val vm:SCViewModel = viewModel()
        val uistate by vm.uidata.collectAsState()
        LazyColumn(modifier = Modifier
            .padding(padding)
            .fillMaxSize()) {
            items(uistate){
                Column(modifier = Modifier
                    .padding(start = 10.dp, end = 10.dp)
                    .fillMaxWidth()
                    .drawBehind {
                        drawRect(
                            color = androidx.compose.ui.graphics.Color.Black, // 选择你需要的颜色
                            topLeft = Offset.Zero,
                            size =this.size,
                            style = Stroke(width = 1.dp.toPx())
                        )
                    }
                    .clickable {
                        Logger.d("q=="+query)
                        if(query == "update"){
                            navCotroller.popBackStack()
                            navCotroller.navigate("update/${it.id}")
                        }else{
                            navCotroller.popBackStack()
                            navCotroller.navigate("course/${it.id}")
                        }
                    }
                    .padding(top = 10.dp, bottom = 10.dp, start = 10.dp), horizontalAlignment = Alignment.Start) {
                    Text(text = "课程名:"+it.name)
                    Text(text = "考察方式:"+it.exam)
                    Text(text = "学分:"+it.credits)
                    Text(text = "学时:"+it.studyhour)
                    Text(text = "老师:"+it.techname)
                }
            }
        }
    }
}